﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DMSMT.Tool
{
    public partial class RDPMainFrm : Form
    {
        public List<List<string>> rdpList = null;

        public RDPMainFrm()
        {
            InitializeComponent();
        }

        private void RDPMainFrm_Load(object sender, EventArgs e)
        {
            if (rdpList.Count == 0)
            {
                return;
            }
            tcMain.TabPages.Clear();
            for (int i = 0; i < rdpList.Count; i++)
            {
                List<string> l = rdpList[i];
                string ip = l[1];

                //创建一个TabPage
                TabPage tabPage = new TabPage();
                //设置选项卡文本
                tabPage.Text = ip;
                //选项卡名字（可以通过这个名字来访问到tabControl1中的选项卡）
                //tabPage.Name = "page1";

                RDPSubFrm page = new RDPSubFrm();
                page.TopLevel = false;
                page.Dock = DockStyle.Fill;
                page.FormBorderStyle = FormBorderStyle.None;
                tabPage.Controls.Add(page);
                page.Server = ip;
                page.UserName = l[2];
                page.Pwd = l[3];

                page.Show();
                page.Connect();

                tcMain.TabPages.Add(tabPage);

                //如果选项卡内的控件比较多，则可以添加一个Form控件，但是Form空间的TopLevel要设置为false
//Form1 page = new Form1();
//page.Name = "formpage"
//page.TopLevel = false;
////给Form去边框
//page.FormBorderStyle = FormBorderStyle.None;
////把page添加到tabPage中
//tabPage.Controls.Add(chatPage);
////在tabPage选项卡中显示出来
//page.Show();
//添加选项卡tabPage到TabControl中
//tabControl1.TabPages.Add(tabPage);
               


                //通过索引访问选项卡
//tabControl1.TabPages[0]
////通过TabPage的Name属性访问选项卡
//tabControl1.Tabpages["page1"]
 
//访问选项卡中的控件
//tabControl1.TabPages[0].Controls[0];
//tabControl1.TabPages[0].Controls["formpage"];
 
//如果要访问控件中的成员，需要进行类型转换
//例如，访问Form1中的成员
//Form1 form = tabControl1.TabPages[0].Controls[0] as Form1;
//然后通过form来访问Form1的内部成员

            }
        }

        private void tcMain_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                for (int i = 0; i < tcMain.TabPages.Count; i++)
                { 
                    if(tcMain.GetTabRect(i).Contains(new Point(e.X,e.Y)))
                    {
                        tcMain.SelectedIndex=i;
                        //TabPage tabPage = tcMain.TabPages[i];
                        //tcMain.SelectedTab=tabPage;
                        //tabPage.ContextMenuStrip = this.cMenu;
                        break;
                    }
                }
                this.tcMain.ContextMenuStrip=this.cMenu;
            }
        }

        private void tcMain_MouseLeave(object sender, EventArgs e)
        {
            this.tcMain.ContextMenuStrip = null;
        }

        private void tcMain_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.tcMain.SelectedTab.Dispose();
        }

        private void tsmiClose_Click(object sender, EventArgs e)
        {
            this.tcMain.SelectedTab.Dispose();
        }

        private void tsmiConnect_Click(object sender, EventArgs e)
        {
            TabPage tabPage = this.tcMain.SelectedTab;
            RDPSubFrm page = (RDPSubFrm)tabPage.Controls[0];
            page.RConnect();
        }
    }
}
